<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\ActivityOrder;
use app\common\model\WithdrawRequest;
use app\common\model\User;
use think\Db;

class Merchant extends Api
{
    protected $noNeedRight = '*';

    public function getCount()
    {
        $data = [
            'order_count'    => 0,
        ];

        $model_order = new ActivityOrder();
        // 总订单数
        $order = $model_order->field('count(*) as order_count')
            ->where('shop_id', $this->auth->id)
            ->find();

        if ($order) {
            $data['order_count']  = $order['order_count'];
        }


        $userInfo = User::where('id', $this->auth->id)->find();
        $userInfo->avatar = cdnurl($userInfo->avatar, true);
        $data['userInfo'] = $userInfo;

        $this->success('success', $data);
    }

    public function getRequestList()
    {
        $user_id = $this->auth->id;
        $model = new WithdrawRequest();
        $list = $model->where('user_id', $user_id)->order('id desc')->paginate(10);

        $this->success('success', $list);
    }

    public function sendRequest()
    {
        $data = [
            'amount'    => input('post.amount'),
            'bank_name' => input('post.bank_name'),
            'bank_card' => input('post.bank_card'),
            'realname'  => input('post.realname'),
            'phone'     => input('post.phone'),
        ];

        $model_user = new User();
        $user = $model_user->where('id', $this->auth->id)->find();

        $model_request = new WithdrawRequest();
        // 查询用户是否有提现记录
        $request = $model_request->where('user_id', $this->auth->id)->where('status', 1)->count();
        if ($request) {
            $this->error('您当前已有一条提现申请正在处理中，暂不接受新的提现申请');
        }

        if ($user['all_withdraw_money'] - $user['done_withdraw_money']  < $data['amount']) {
            $this->error('余额不足');
        }

        if ($data['amount'] < 0) {
            $this->error('提现金额错误');
        }

        if (empty($data['bank_name'])) {
            $this->error('银行名称不能为空');
        }

        if (empty($data['bank_card'])) {
            $this->error('银行卡号不能为空');
        }

        if (empty($data['realname'])) {
            $this->error('真实姓名不能为空');
        }

        if (empty($data['phone'])) {
            $this->error('手机号不能为空');
        }

        $before_amount = $user['all_withdraw_money'] - $user['done_withdraw_money'];
        $after_amount  = $user['all_withdraw_money'] - $user['done_withdraw_money'] - $data['amount'];

        Db::startTrans();
        try {
            $model_request->save([
                'user_id'       => $this->auth->id,
                'amount'        => $data['amount'],
                'bank_name'     => $data['bank_name'],
                'bank_card'     => $data['bank_card'],
                'realname'      => $data['realname'],
                'phone'         => $data['phone'],
                'before_amount' => $before_amount,
                'after_amount'  => $after_amount,
            ]);

            $model_user->money(-$data['amount'], $this->auth->id, '提现申请');

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error('提交失败');
        }

        $this->success('提交成功', ['money' => $after_amount]);
    }
}
